package com.manu.mdatepicker;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static com.manu.mdatepicker.Util.getScreenWidth;

/**
 * 年龄选择器
 */
public class AgePickerDialog extends Dialog implements MPickerView.OnSelectListener, View.OnClickListener {

    private Context mContext;

    private MPickerView mpvDialogMinAge;
    private MPickerView mpvDialogMaxAge;

    private TextView tvDialogTitle;
    private TextView tvDialogTopCancel;
    private TextView tvDialogTopConfirm;
    private TextView tvDialogBottomCancel;
    private TextView tvDialogBottomConfirm;
    private LinearLayout llDialogBottom;
    private LinearLayout llDialog;


    private String mTitle;
    private int mGravity;
    private boolean isCanceledTouchOutside;
    private float mConfirmTextSize;
    private float mCancelTextSize;
    private int mConfirmTextColor;
    private int mCancelTextColor;
    private OnDateResultListener mOnDateResultListener;

    private AgePickerDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_age_picker);
        initView();
        initData();
    }

    private void initView() {
        mpvDialogMinAge = findViewById(R.id.mpvDialogMinLevel);
        mpvDialogMaxAge = findViewById(R.id.mpvDialogMaxLevel);

        tvDialogTitle = findViewById(R.id.tvDialogTitle);

        tvDialogTopCancel = findViewById(R.id.tvDialogTopCancel);
        tvDialogTopConfirm = findViewById(R.id.tvDialogTopConfirm);
        tvDialogBottomCancel = findViewById(R.id.tvDialogBottomCancel);
        tvDialogBottomConfirm = findViewById(R.id.tvDialogBottomConfirm);
        llDialogBottom = findViewById(R.id.llDialogBottom);
        llDialog = findViewById(R.id.llDialog);

        mpvDialogMinAge.setOnSelectListener(this);
        mpvDialogMaxAge.setOnSelectListener(this);

        tvDialogTopCancel.setOnClickListener(this);
        tvDialogTopConfirm.setOnClickListener(this);
        tvDialogBottomCancel.setOnClickListener(this);
        tvDialogBottomConfirm.setOnClickListener(this);
    }


    /**
     * 设置初始值
     *
     * @param mCurrentMinAge 当前最小年龄
     * @param mCurrentMaxAge 当前最大年龄
     */
    public void setDefaultValue(int mCurrentMinAge, int mCurrentMaxAge) {
        this.mCurrentMinAge = mCurrentMinAge;
        this.mCurrentMaxAge = mCurrentMaxAge;
        UpdateData();
    }

    private int mMinAge = 0;
    private int mMaxAge = 99;
    private int mCurrentMinAge = 20;
    private int mCurrentMaxAge = 30;

    private void UpdateData() {
        mpvDialogMinAge.setDefaultValue(String.valueOf(mCurrentMinAge));
        mpvDialogMaxAge.setDefaultValue(String.valueOf(mCurrentMaxAge));
        mpvDialogMinAge.getSelectValue();
        mpvDialogMaxAge.getSelectValue();
    }
    private void initData() {
        mpvDialogMinAge.setText(mContext.getString(R.string.strAge));
        mpvDialogMaxAge.setText(mContext.getString(R.string.strAge));

        List<String> mListMinAge = new ArrayList<>();
        List<String> mListMaxAge = new ArrayList<>();

        for (int i = mMinAge; i <= mMaxAge; i++) {
            mListMinAge.add(String.valueOf(i));
            mListMaxAge.add(String.valueOf(i));
        }

        mpvDialogMinAge.setData(mListMinAge);
        mpvDialogMaxAge.setData(mListMaxAge);

        UpdateData();

        // Setting
        if (!TextUtils.isEmpty(mTitle)) tvDialogTitle.setText(mTitle);

        Window window = getWindow();
        WindowManager.LayoutParams params = Objects.requireNonNull(window).getAttributes();
        params.gravity = mGravity;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setBackgroundDrawableResource(android.R.color.transparent);

        setCanceledOnTouchOutside(isCanceledTouchOutside);

        if (mGravity == Gravity.BOTTOM) {
            params.width = WindowManager.LayoutParams.MATCH_PARENT;
            llDialogBottom.setVisibility(View.GONE);
            llDialog.setBackgroundResource(R.drawable.dialog_date_picker_bottom_bg);
        } else if (mGravity == Gravity.CENTER) {
            params.width = (int) (getScreenWidth((Activity) mContext) * 8 / 9);
            tvDialogTopCancel.setVisibility(View.GONE);
            tvDialogTopConfirm.setVisibility(View.GONE);
            llDialog.setBackgroundResource(R.drawable.dialog_date_picker_center_bg);
        } else {
            params.width = (int) (getScreenWidth((Activity) mContext) * 8 / 9);
            tvDialogTopCancel.setVisibility(View.GONE);
            tvDialogTopConfirm.setVisibility(View.GONE);
            llDialog.setBackgroundResource(R.drawable.dialog_date_picker_center_bg);
        }


        if (mConfirmTextSize != 0.0f && mConfirmTextSize != -1.0f) {
            tvDialogTopConfirm.setTextSize(mConfirmTextSize);
            tvDialogBottomConfirm.setTextSize(mConfirmTextSize);
        }

        if (mConfirmTextColor != 0 && mConfirmTextColor != -1) {
            tvDialogTopConfirm.setTextColor(mConfirmTextColor);
            tvDialogBottomConfirm.setTextColor(mConfirmTextColor);
        }

        if (mCancelTextSize != 0.0f && mCancelTextSize != -1.0f) {
            tvDialogTopCancel.setTextSize(mCancelTextSize);
            tvDialogBottomCancel.setTextSize(mCancelTextSize);
        }

        if (mCancelTextColor != 0 && mCancelTextColor != -1) {
            tvDialogTopCancel.setTextColor(mCancelTextColor);
            tvDialogBottomCancel.setTextColor(mCancelTextColor);
        }
        window.setAttributes(params);
    }


    @Override
    public void onSelect(View v, String data) {
        if (v.getId() == R.id.mpvDialogMinLevel) {
            mCurrentMinAge = Integer.parseInt(data);
        } else if (v.getId() == R.id.mpvDialogMaxLevel) {
            mCurrentMaxAge = Integer.parseInt(data);
        }
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.tvDialogTopCancel || i == R.id.tvDialogBottomCancel) {
            dismiss();

        } else if (i == R.id.tvDialogTopConfirm || i == R.id.tvDialogBottomConfirm) {
            if (mOnDateResultListener != null) {
                mOnDateResultListener.onDateResult(mCurrentMinAge, mCurrentMaxAge);
                dismiss();

            }
        }
    }

    public void setOnDateResultListener(OnDateResultListener onDateResultListener) {
        this.mOnDateResultListener = onDateResultListener;
    }


    public static class Builder {
        private Context mContext;
        private String mTitle;
        private int mGravity;
        private boolean isCanceledTouchOutside;
        private float mConfirmTextSize;
        private float mCancelTextSize;
        private int mConfirmTextColor;
        private int mCancelTextColor;
        private OnDateResultListener mOnDateResultListener;

        public Builder(Context mContext) {
            this.mContext = mContext;
        }

        public Builder setTitle(String mTitle) {
            this.mTitle = mTitle;
            return this;
        }

        public Builder setGravity(int mGravity) {
            this.mGravity = mGravity;
            return this;
        }

        public Builder setCanceledTouchOutside(boolean canceledTouchOutside) {
            isCanceledTouchOutside = canceledTouchOutside;
            return this;
        }

        public Builder setConfirmStatus(float textSize, int textColor) {
            this.mConfirmTextSize = textSize;
            this.mConfirmTextColor = textColor;
            return this;
        }

        public Builder setCancelStatus(float textSize, int textColor) {
            this.mCancelTextSize = textSize;
            this.mCancelTextColor = textColor;
            return this;
        }

        public Builder setOnDateResultListener(OnDateResultListener onDateResultListener) {
            this.mOnDateResultListener = onDateResultListener;
            return this;
        }

        private void applyConfig(AgePickerDialog dialog) {
            if (this.mGravity == 0) this.mGravity = Gravity.CENTER;
            dialog.mContext = this.mContext;
            dialog.mTitle = this.mTitle;
            dialog.mGravity = this.mGravity;
            dialog.mConfirmTextSize = this.mConfirmTextSize;
            dialog.mConfirmTextColor = this.mConfirmTextColor;
            dialog.mCancelTextSize = this.mCancelTextSize;
            dialog.mCancelTextColor = this.mCancelTextColor;
            dialog.isCanceledTouchOutside = this.isCanceledTouchOutside;
            dialog.mOnDateResultListener = this.mOnDateResultListener;
        }

        public AgePickerDialog build() {
            AgePickerDialog dialog = new AgePickerDialog(mContext);
            applyConfig(dialog);
            return dialog;
        }
    }

    public interface OnDateResultListener {
        void onDateResult(int min_age, int max_age);
    }
}
